package com.wlstock.funi.MissionInstance;

import android.content.Context;

import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.em.mission.Mission;
import com.em.mission.MissionItem;
import com.em.mission.MissionListener;
import com.em.mission.MissionResultListener;
import com.wlstock.funi.R;
import com.wlstock.funi.constant.SharePrefConstants;
import com.wlstock.funi.constant.URLConstant;
import com.wlstock.funi.request.CustomStringRequest;
import com.wlstock.funi.request.EventBusConstants;
import com.wlstock.funi.request.NetWorkListener;
import com.wlstock.funi.utils.NotiIdGenerator;
import com.wlstock.funi.utils.NotiUtils;
import com.wlstock.funi.utils.SharePreferenceUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.wlstock.funi.constant.URLConstant.DF_MARKET_STA;


/**
 * 东方财富全市场统计分析
 */
public class MarketStaticMission extends MissionItem implements MissionListener, NetWorkListener {

    private CustomStringRequest dfcfReq;
    private String content;

    private static MarketStaticMission instance;


    public static MarketStaticMission getInstance(Context mContext,String name) {
        if (instance == null) {
            synchronized (MarketStaticMission.class) {
                if (instance == null) {
                    instance = new MarketStaticMission(mContext,name);
                }
            }
        }
        return instance;
    }


    private MarketStaticMission(Context mContext,String name ) {
        super(mContext ,name );
    }


    @Override
    public void start(MissionResultListener resultListener) {
        this.resultListener = resultListener;
        start(this, resultListener);
    }

    @Override
    public void beforeExecuteListener(Mission mission) {

    }

    @Override
    public void onExecute(Mission mission) {
        content = "";
        dfcfReq();
    }

    private void dfcfReq(){
        dfcfReq = new CustomStringRequest();
        dfcfReq.networkResponseListener = this;
        dfcfReq.request(Request.Method.GET, URLConstant.DF_HOST, null, null, DF_MARKET_STA, DF_MARKET_STA);
    }

    @Override
    public void afterExecute(Mission mission) {

    }

    @Override
    public void onErrorResponse(VolleyError error, EventBusConstants eventCode) {

    }

    @Override
    public void onErrorResponse(VolleyError error, String eventCode) {
        if (DF_MARKET_STA.equals(eventCode)){
            if (resultListener!=null){
                resultListener.executeFaile(this);
            }
        }
    }

    @Override
    public void onResponse(String response, EventBusConstants eventCode) {

    }

    @Override
    public void onResponse(String response, String eventCode) {
        if (DF_MARKET_STA.equals(eventCode)){
            parseDFMarketSta(response);
            if (resultListener!=null){
                JSONObject obj = new JSONObject();
                try {
                    obj.put("content",content);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                resultListener.executeSuccess(obj, this);
            }
        }
    }

    private void parseDFMarketSta(String response) {
        if (response==null || response.isEmpty()) {
            return;
        }
        Pattern compile = Pattern.compile("(\"[0-9]{7}(.)*\")");
        Matcher matcher = compile.matcher(response);
        String result = "";
        while(matcher.find()){
            result = response.substring(matcher.start(), matcher.end());
        }
        String[] results = result.split(",\"");

        StringBuffer sbContent = new StringBuffer();

        int up =  -1 ;
        int all = -1 ;
        if (results!=null && results.length>1) {
            String[] details = results[0].split(",");
            if (details!=null && details.length==8) {
                String shDetail = details[6];
                String[] shDetailSplit = shDetail.split("\\|");
                if (shDetailSplit!=null && shDetailSplit.length==4) {
                    String shUp = shDetailSplit[0];
                    String shNor = shDetailSplit[1];
                    String shDown = shDetailSplit[2];
                    sbContent.append("上证").append("上涨"+shUp+"家 ").append("平盘"+shNor+"家 ").append("下跌"+shDown+"家 ").append("\n");
                    up  = up  + Integer.valueOf(shUp);
                    all = all + Integer.valueOf(shUp)+Integer.valueOf(shDown);
                }

                String szDetail = details[7];
                String[] szDetailSplit = szDetail.split("\\|");
                if (szDetailSplit!=null && szDetailSplit.length==4) {
                    String szUp = szDetailSplit[0];
                    String szNor = szDetailSplit[1];
                    String szDown = szDetailSplit[2];
                    sbContent.append("深圳").append("上涨 "+szUp+"家 ").append("平盘"+szNor+"家 ").append("下跌"+szDown+"家 ");

                    up  = up  + Integer.valueOf(szUp);
                    all = all + Integer.valueOf(szUp)+Integer.valueOf(szDown);
                }
            }
        }
        content = sbContent.toString();
        NotiUtils.showSimpleNoti(mContext, NotiIdGenerator.ID_MARKET_STA_COUNT, "统计", sbContent.toString(), R.drawable.ic_market_sta);
        if (up!=-1 && all!= -1 ){
            SharePreferenceUtils.setFloatSP(mContext, SharePrefConstants.MARKET_0ZF_PCT, ((float) up)/all);
        }

    }



}
